package com.java46.struts2.order.dao;

import com.java46.struts2.base.entity.TDishesEntity;
import com.java46.struts2.base.entity.TOrderEntity;
import com.java46.struts2.base.entity.TOrderdetailEntity;
import com.java46.struts2.base.util.HibernateUtils;
import com.java46.struts2.base.util.JsonUtils;
import com.java46.struts2.base.util.SqlUtils;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.Query;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName OrderDao
 * @Description 订单数据处理
 * @Author zuxia
 * @Date 2018/7/18 9:54
 * @Vcrsion 1.0
 **/
public class OrderDao {

    /**
    *@Author xiao
    *@Description //分页查询可添加关键字
    *@Date 10:01 2018/7/18
    *@Param []
    *@return List
    */
    public static List queryOrder(String Keyword,int Pageno,int Pagesize){
        String hql="";
        Query query=null;
        if(Keyword==null||Keyword.trim().equals("")){
            hql="from TOrderEntity t inner join TUserEntity u on t.uId=u.uId";
            //query=HibernateUtils.getQuery(hql).setFirstResult((Pageno-1)*Pagesize).setMaxResults(Pagesize);
        }else {
            hql="from TOrderEntity t inner join TUserEntity u on t.uId=u.uId where orderNo like '%"+Keyword+"%'";

            //query.setParameter(1,Keyword);
        }
        query=HibernateUtils.getQuery(hql).setFirstResult((Pageno-1)*Pagesize).setMaxResults(Pagesize);
        List list = query.list();
        return list;
    }

    /**
    *@Author xiao
    *@Description //查询数据总条数
    *@Date 19:27 2018/7/18
    *@Param [Keyword]
    *@return int
    */
    public static int queryOrderCount(String Keyword){
        String hql="";
        Query query=null;
        if(Keyword==null||Keyword.trim().equals("")){
            hql="select count(*) from TOrderEntity";
            //query=HibernateUtils.getQuery(hql);
        }else {
            hql="select count(*) from TOrderEntity where orderNo like '%"+Keyword+"%'";

            //query.setParameter(1,Keyword);
        }
        query=HibernateUtils.getQuery(hql);
        Object o = query.uniqueResult();
        return Integer.parseInt(o.toString());
    }

    /**
    *@Author xiao
    *@Description //根据订单编号删除订单
    *@Date 15:04 2018/7/18
    *@Param [orderid]
    *@return void
    */
    public static void deleteOrderOrderid(int orderid){
        Session session=HibernateUtils.getSession();
        Transaction transaction = session.beginTransaction();
        String hql="delete from TOrderdetailEntity where orderId=?1";
        Query query=session.createQuery(hql);
        query.setParameter(1,orderid);
        query.executeUpdate();
        String hql1="delete from TOrderEntity where orderId=?1";
        query=session.createQuery(hql1);
        query.setParameter(1,orderid);
        query.executeUpdate();
        transaction.commit();
        session.close();
    }


    /**
     * @Author xiaobo
     * @Description //查询用户订单可根据状态查询
     * @Date 23:20 2018/7/19 0019
     * @Param [uSign, struts]
     * @return java.util.List
     */
    public static List queryUserOrder(int uId,int struts){
        String hql="";
        Query query=null;
        if(struts==3){

            hql="from TOrderEntity od join TOrderdetailEntity odd on od.orderId=odd.orderId join TDishesEntity de on odd.dId=de.dId where od.uId=?1";
            query=HibernateUtils.getQuery(hql);
            query.setParameter(1,uId);
        }else {
            hql="from TOrderEntity od join TOrderdetailEntity odd on od.orderId=odd.orderId join TDishesEntity de on odd.dId=de.dId where od.uId=?1 and od.struts=?2";
            query=HibernateUtils.getQuery(hql);
            query.setParameter(1,uId);
            query.setParameter(2,struts);
        }
        List list = query.list();
        for (int i = 0; i <list.size() ; i++) {
            for (int j = i+1; j <list.size() ; j++) {

                Object [] obj=(Object [])list.get(i);
                int orderid=((TOrderEntity)obj[0]).getOrderId();
                Object [] obj1=(Object [])list.get(j);
                int orderid1=((TOrderEntity)obj1[0]).getOrderId();
                if(orderid==orderid1){
                    try{
                        TDishesEntity tDishesEntity = (TDishesEntity) obj[2];
                        TDishesEntity tDishesEntity1 = (TDishesEntity) obj1[2];
                        List Listobj = new ArrayList();
                        Listobj.add(tDishesEntity);
                        Listobj.add(tDishesEntity1);
                        obj[2]=null;
                        obj[2]=Listobj;
                        list.remove(j);
                        j--;
                    }catch (Exception e){
                        List listobj1 = (ArrayList) obj[2];
                        TDishesEntity tDishesEntity3 = (TDishesEntity) obj1[2];
                        listobj1.add(tDishesEntity3);
                        obj[2]=null;
                        obj[2]=listobj1;
                        list.remove(j);
                        j--;
                    }
                }
            }
        }
        return list;
    }


    /**
    *@Author xiao
    *@Description //添加订单
    *@Date 16:50 2018/7/20
    *@Param [orderEntity, list]
    *@return void
    */
    public static String addOrder(TOrderEntity orderEntity, List list){
        Session session=HibernateUtils.getSession();
        Transaction transaction=session.beginTransaction();
        session.save(orderEntity);
        transaction.commit();
        List list1 = queryOrderNo(orderEntity.getOrderNo());
        Object [] o = (Object[]) list1.get(0);
        Integer orderId = ((TOrderEntity)o[0]).getOrderId();
        session.close();
        for (int i = 0; i <list.size() ; i++) {
            session = HibernateUtils.getSession();
            transaction = session.beginTransaction();
            TOrderdetailEntity  orderdetail = (TOrderdetailEntity) list.get(i);
            orderdetail.setoDId(0);
            orderdetail.setCooked(0);
            orderdetail.setOrderId(orderId);
            session.save(orderdetail);
            transaction.commit();
            session.close();
        }
        return orderEntity.getOrderNo();
    }

    /**
    *@Author xiao
    *@Description //根据订单号查询订单
    *@Date 16:56 2018/7/20
    *@Param [orderNo]
    *@return java.util.List
    */
    public static List queryOrderNo(String orderNo){
        String hql="";
        Query query=null;
            hql="from TOrderEntity od left join TOrderdetailEntity odd on od.orderId=odd.orderId left join TDishesEntity de on odd.dId=de.dId where od.orderNo=?1";
            query=HibernateUtils.getQuery(hql);
            query.setParameter(1,orderNo);
        List list = query.list();
        return list;
    }


   /**
   *@Author xiao
   *@Description //修改订单状态
   *@Date 19:28 2018/7/20
   *@Param [orderId, payType, Struts]
   *@return void
   */
    public static void updateOrderState(int orderId,int payType,int Struts){

        Session session=HibernateUtils.getSession();
        Transaction transaction=session.beginTransaction();
        TOrderEntity orderEntity=session.get(TOrderEntity.class,orderId);
        if(payType==9){
            orderEntity.setStruts(Struts);;
        }else{
            orderEntity.setStruts(Struts);
            orderEntity.setPayType(payType);
        }
        session.update(orderEntity);
        transaction.commit();
        session.close();

    }
}
